﻿#region

using System;
using System.Web.UI;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService
{
    public partial class RequestPassword : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnLogin_OnClick(object sender, EventArgs e)
        {
            if (IsValid)
            {
                User user = UserBLL.GetUserByUsername(txtUsername.Text);
                if (user == null)
                {
                    lblStatus.Text = "Invalid username";
                }
                else
                {
                    Guid uniqueId = UserBLL.RequestResetPassword(txtUsername.Text);

                    string link = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath +
                                  "/ResetPassword.aspx?guid=" + uniqueId;

                    string eMail = null;
                    string name = null;

                    switch (user.Type)
                    {
                        case "Administrator":
                            Administrator admin = AdministratorBLL.GetAdministrator(user.Username);
                            Staff staff = StaffBLL.GetStaffById(admin.StaffId);

                            eMail = staff.EMail;
                            name = staff.FirstName;
                            break;

                        case "Customer":
                            Customer customer = new Customer {Username = user.Username};
                            CustomerBLL.GetCustomerId(customer);
                            CustomerBLL.GetCustomerType(customer);

                            switch (customer.Type)
                            {
                                case "Personal":
                                    PersonalCustomer personalCustomer = new PersonalCustomer
                                    {
                                        CustomerId = customer.CustomerId
                                    };
                                    PersonalCustomerBLL.GetPersonalCustomerDetails(personalCustomer);
                                    eMail = personalCustomer.EMail;
                                    name = personalCustomer.FirstName;
                                    break;
                                case "Business":
                                    BusinessCustomer businessCustomer = new BusinessCustomer
                                    {
                                        CustomerId = customer.CustomerId
                                    };
                                    BusinessCustomerBLL.GetBusinessCustomerDetails(businessCustomer);
                                    eMail = businessCustomer.EMail;
                                    name = businessCustomer.CompanyName;
                                    break;
                            }
                            break;
                    }

                    UserBLL.SendResetPasswordLink(link, uniqueId, name, eMail);

                    Response.Redirect("ResetLinkSentMessage.aspx");
                }
            }
        }
    }
}